/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.app.rest.runtime.cus;

import cmbchina.util.AjaxJson;
import org.flowable.app.domain.editor.FlowForm;
import org.flowable.app.repository.editor.CusFlowFormRepository;
import org.flowable.app.service.exception.BadRequestException;
import org.flowable.app.service.runtime.FlowableTaskFormService;
import org.flowable.app.service.runtime.FlowableTaskQueryService;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.impl.HistoryServiceImpl;
import org.flowable.engine.impl.RepositoryServiceImpl;
import org.flowable.engine.impl.TaskServiceImpl;
import org.flowable.form.api.FormRepositoryService;
import org.flowable.form.api.FormService;
import org.flowable.form.model.FormModel;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

//import org.flowable.app.model.runtime.SaveFormRepresentation;

/**
 * 流程运行的状态
 * @author Joram Barrez
 */
@RestController
@RequestMapping("/rest/task-run")
public class CusTaskRunResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(CusTaskRunResource.class);
    @Autowired
    protected FlowableTaskFormService taskFormService;
    @Autowired
    protected TaskServiceImpl taskService;
    @Autowired
    protected HistoryServiceImpl historyService;
    @Autowired
    protected RepositoryServiceImpl repositoryService;
    @Autowired
    protected FlowableTaskQueryService taskQueryService;
    @Autowired
    protected FormService formService;

    @Autowired
    protected RuntimeService runtimeService;
    @Autowired
    protected FormRepositoryService formRepositoryService;

    @Autowired
    protected CusFlowFormRepository cusFlowFormRepository;


    /**
     * cus function 用来根据节点的自定义id来查询任务的日志
     * @param taskDefinitionKey
     * @return
     */
    @RequestMapping(value = "/taskLog/{taskDefinitionKey}/{processInstId}", method = RequestMethod.GET, produces = "application/json")
    public AjaxJson getTaskLogByElemntId(@PathVariable String taskDefinitionKey,@PathVariable String processInstId) {
        AjaxJson ajaxJson = new AjaxJson();

        FlowForm flowForm = new FlowForm();
        flowForm.setTaskDefinitionKey(taskDefinitionKey);
        flowForm.setProcessInstId(processInstId);
        List<FlowForm> flowForms = cusFlowFormRepository.selectFlowFormByProcessInstIdAndTaskDefinitionKey(flowForm);
        String str = "当前无日志";
        if(flowForms!=null&&flowForms.size()>0){
            flowForm = flowForms.get(0);
            if(flowForm.getScriptCommand()!=null&&!"".equals(flowForm.getScriptCommand())){
                str = flowForm.getScriptCommand() + "<br>" + flowForm.getResult();
            }else{
                str = flowForms.get(0).getResult();
            }
        }
        ajaxJson.setMsg(str);
        return ajaxJson;
    }

}
